package com.liciecruzadinha.app;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

/**
 * Created by bruna on 22/05/14.
 */
public class EventoVerificarEditText implements TextWatcher{
    private EditText edt;
    private Activity a;


    public EventoVerificarEditText(EditText edt, Activity a){
        this.edt = edt;
        this.a = a;
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        switch (edt.getId()){
            case R.id.edt72:
                verificar(PalavrasConstants.PERIODO,"PERIODO");
                break;
            case R.id.edt105:
                verificar(PalavrasConstants.MAQUILLAGE,"MAQUILLAGE");
                break;
            case R.id.edt57:
                verificar(PalavrasConstants.SOUTIEN,"SOUTIEN");
                break;
            case R.id.edt29:
                verificar(PalavrasConstants.BAGAGE,"BAGAGE");
                break;
            case R.id.edt63:
                verificar(PalavrasConstants.IODO,"IODO");
                break;
            case R.id.edt49:
                verificar(PalavrasConstants.NEON,"NEON");
                break;
            case R.id.edt66:
                verificar(PalavrasConstants.GASES,"GASES");
                break;
            case R.id.edt89:
                verificar(PalavrasConstants.CAVIAR,"CAVIAR");
                break;
            case R.id.edt88:
                verificar(PalavrasConstants.CAVIAR,"CAVIAR");
                break;
            case R.id.edt99:
                verificar(PalavrasConstants.BORO,"BORO");
                break;
            case R.id.edt73:
                verificar(PalavrasConstants.FLUOR,"FLUOR");
                break;
            case R.id.edt102:
                verificar(PalavrasConstants.CA,"CA");
                break;
            case R.id.edt107:
                verificar(PalavrasConstants.PASTEUR,"PASTEUR");
                break;
            case R.id.edt39:
                verificar(PalavrasConstants.O,"O");
                break;
        }

    }

    @Override
    public void afterTextChanged(Editable editable) {

    }

    private void verificar(int[] palavraConst, String palavra){
        String verificar = "";
        int fieldsClean = 0;
        EditText edtVerificar = null;

        for(int i= 0; i<palavraConst.length; i++){
            edtVerificar = (EditText)a.findViewById(palavraConst[i]);
            verificar+=edtVerificar.getText().toString();
            if(edtVerificar.getText().toString().equals(""))
                fieldsClean++;
        }
        if((fieldsClean<=1 && edtVerificar.getId() == edt.getId()) || (!edtVerificar.isEnabled() && edtVerificar.getId() != edt.getId()))
            if (verificar.toUpperCase().equals(palavra.toUpperCase())){
                Log.d("Status","Acertou");
                desabilitarSeqEditText(palavraConst);
                return;
            }else {
                Log.d("Status", "Não acertou");
                limparSeqEditText(palavraConst);
            }
    }

    private void desabilitarSeqEditText(int[] palavraConst){
        for(int i= 0; i<palavraConst.length; i++){
            ((EditText)a.findViewById(palavraConst[i])).setEnabled(false);
        }
    }

    private void limparSeqEditText(int[] palavraConst){

        for(int i= 0; i<palavraConst.length; i++){
            EditText edtSeq = (EditText)a.findViewById(palavraConst[i]);

            if(edtSeq.isEnabled()) {
                edtSeq.setText("");
                edtSeq.setFocusable(false);
            }
        }
    }
}
